#include "config_parse.h"
#include <stdio.h>

 int main() { 
    struct config_list list;
    config_parse_init(&list);
    printf("===========config_parse_file============\n");
    int total = config_parse_file(&list, "demo_config.ini");
    printf("total = %d\n", total);
    struct config_list *cur = &list;
    for (int i = 1; i <= total; i++)
    {
        cur = config_parse_get_line(&list, i);
        printf("%d: key = %s, value = %s\n", i, cur->key, cur->value);
    }
    printf("2ello value is %s\n", config_parse_get_value_by_key(&list, "2ello", NULL));
    printf("topic1 network value is %s\n", config_parse_get_value_by_key(&list, "network", "topic2"));
    int num=1;
    do {
        cur = config_parse_get_topic(&list, num);
        if (cur) printf("%d topic is %s\n", num, cur->value);
        num++;
    } while (cur);
    
    num=1;
    do {
        cur = config_parse_get_topic_line(&list, "topic1", num);
        if (cur) printf("%d topic1 is %s %s\n", num, cur->key, cur->value);
        num++;
    } while (cur);

    config_parse_free(&list);
    return 0;

    printf("===========config_parse_buffer============\n");
    FILE *fp = fopen("demo_config.ini", "r");
    if (!fp) {
        perror("fopen");
        return -1;
    }
    char buffer[2048];
    int i = fread(buffer, 1, sizeof(buffer), fp);
    fclose(fp);
    printf("i = %d\n", i);
    buffer[i] = 0;
    total = config_parse_buffer(&list, buffer);
    printf("total = %d\n", total);
    cur = &list;
    for (int i = 1; i <= total; i++)
    {
        cur = config_parse_get_line(&list, i);
        printf("%d: key = %s, value = %s\n", i, cur->key, cur->value);
    }
    printf("hello value is %s\n", config_parse_get_value_by_key(&list, "hello", NULL));

    config_parse_add_empty(&list);  
    config_parse_add_note(&list, "this is test note");
    config_parse_add_line(&list, "key_no_space", "\"value is value\"");
    config_parse_create_file(&list, "createfile.ini");
    config_parse_free(&list);
    
    return 0;
}